
local Rank, Session, Top10, Spectate = false, false, false, false
local rank, session, spectate
local top10 = {}

usermessage.Hook("gStats.Rank", function( um )
	if Session then Session = false end
	if Top10 then Top10 = false end
	local Table = um:ReadString() 
	local Args = string.Explode(";", Table)
	rank = Args
	Rank = true
	timer.Simple(5, function()
		Rank = false
	end)
end)

usermessage.Hook("gStats.Session", function( um )
	if Rank then Rank = false end
	if Top10 then Top10 = false end
	local Table = um:ReadString() 
	local Args = string.Explode(";", Table)
	session = Args
	Session = true
	timer.Simple(5, function()
		Session = false
	end)
end)

usermessage.Hook("gStats.Top10", function( um )
	if Rank then Rank = false end
	if Session then Session = false end
	local name = um:ReadString()
	local points = um:ReadFloat()
	timer.Simple(2, function()
		Top10 = true
	end)
	timer.Simple(10, function()
		Top10 = false
	end)
	table.insert(top10, name .. " (" .. points .. ")") 
end)

usermessage.Hook("gStats.Spectate", function( um )
	print("gStats.Spectate")
	if Rank then Rank = false end
	if Session then Session = false end
	if Top10 then Top10 = false end
	local Table = um:ReadString() 
	local Args = string.Explode(";", Table)
	spectate = Args
	Spectate = true
	PrintTable(spectate)
end)


usermessage.Hook("gStats.AllowSpectate", function( um )
	print("spec")
	Spectate = um:ReadBool()
end)

usermessage.Hook("gStats.Clear", function( um )
	table.Empty(top10)
end)

usermessage.Hook( "AddText", function( um )
	local argc = um:ReadShort( )
	local args = { }
	for i = 1, argc / 2, 1 do
		table.insert( args, Color( um:ReadShort( ), um:ReadShort( ), um:ReadShort( ), um:ReadShort( ) ) )
		table.insert( args, um:ReadString( ) )
	end
	chat.AddText( unpack( args ) )
end)

hook.Add("HUDPaint", "gStats.HUDPaint", function()
	if Rank then
		surface.SetDrawColor(50, 50, 50, 150)
		surface.DrawRect(4, 100, 165, 75)
		// Session
		surface.SetTextColor(227, 168, 105, 255)
		surface.SetTextPos(8, 105)
		surface.DrawText( "Rank" )
		
		// Position
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 120)
		surface.DrawText(rank[1] .. " Position")
		
		// Points
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 135)
		surface.DrawText(rank[2] .. " Points")
		
		// Kills and Deaths
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 150)
		surface.DrawText("K: " .. rank[3] .. " D: " .. rank[4])
		
		--[[// Headshots
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 165)
		surface.DrawText(1 .. " Headshots (" .. 33 .. "%)")
		
		// Accuracy
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 180)
		surface.DrawText(39 .. "% Accuracy")
		
		// Play Time
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 195)
		surface.DrawText("Time " .. 0 .. "d " .. 00 .. "h " .. 06 .. "m " .. 08 .. "s")--]]
	elseif Session then
		surface.SetDrawColor(50, 50, 50, 150)
		surface.DrawRect(4, 100, 165, 75)
		// Session
		surface.SetTextColor(227, 168, 105, 255)
		surface.SetTextPos(8, 105)
		surface.DrawText( "Session" )
		
		// Position
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 120)
		surface.DrawText(session[1] .. " Position")
		
		// Points
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 135)
		surface.DrawText(session[2] .. " Points")
		
		// Kills and Deaths
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 150)
		surface.DrawText("K: " .. session[3] .. " D: " .. session[4])
		
		--[[// Headshots
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 165)
		surface.DrawText(1 .. " Headshots (" .. 33 .. "%)")
		
		// Accuracy
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 180)
		surface.DrawText(39 .. "% Accuracy")
		
		// Play Time
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 195)
		surface.DrawText("Time " .. 0 .. "d " .. 00 .. "h " .. 06 .. "m " .. 08 .. "s")--]]
	elseif Top10 then
		surface.SetDrawColor(50, 50, 50, 150)
		surface.DrawRect(4, 100, 225, 150)
		// Session
		surface.SetTextColor(227, 168, 105, 255)
		surface.SetTextPos(8, 105)
		surface.DrawText( "Top10" )
		
		// Player 1
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 120)
		surface.DrawText("1. " .. top10[1])
		
		// Player 2
		if table.Count(top10) >= 2 then
			surface.SetTextColor(238, 223, 204, 255)
			surface.SetTextPos(12, 135)
			surface.DrawText("2. " .. top10[2])
		end
		
		// Player 3
		if table.Count(top10) >= 3 then
			surface.SetTextColor(238, 223, 204, 255)
			surface.SetTextPos(12, 150)
			surface.DrawText("3. " .. top10[3])
		end
		
		// Player 4
		if table.Count(top10) >= 4 then
			surface.SetTextColor(238, 223, 204, 255)
			surface.SetTextPos(12, 165)
			surface.DrawText("4. " .. top10[4])
		end
		
		// Player 5
		if table.Count(top10) >= 6 then
			surface.SetTextColor(238, 223, 204, 255)
			surface.SetTextPos(12, 180)
			surface.DrawText("5. " .. top10[5])
		end
		
		// Player 6
		if table.Count(top10) >= 6 then
			surface.SetTextColor(238, 223, 204, 255)
			surface.SetTextPos(12, 195)
			surface.DrawText("6. " .. top10[6])
		end
	end
	if !LocalPlayer():Alive() and LocalPlayer():GetObserverMode() == OBS_MODE_CHASE and LocalPlayer():GetObserverTarget() and LocalPlayer():GetObserverTarget():IsValid() and LocalPlayer():GetObserverTarget():IsPlayer() and LocalPlayer():GetObserverTarget() != LocalPlayer() and Spectate then
		surface.SetDrawColor(50, 50, 50, 150)
		surface.DrawRect(4, 100, 165, 75)
		// Session
		surface.SetTextColor(227, 168, 105, 255)
		surface.SetTextPos(8, 105)
		surface.DrawText( LocalPlayer():GetObserverTarget():Nick() .. "'s Stats" )
		
		// Position
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 120)
		surface.DrawText(spectate[1] .. " Position")
		
		// Points
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 135)
		surface.DrawText(spectate[2] .. " Points")
		
		// Kills and Deaths
		surface.SetTextColor(238, 223, 204, 255)
		surface.SetTextPos(12, 150)
		surface.DrawText("K: " .. spectate[3] .. " D: " .. spectate[4])
	end
end)
